package com.cloud.user.biz.login.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cloud.excption.BizException;
import com.cloud.mall.user.TbMember;
import com.cloud.user.biz.dto.LoginDto;
import com.cloud.user.biz.dto.LoginType;
import com.cloud.user.biz.login.AbstractLogin;
import com.cloud.user.service.ITbMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author tangyuan2meicloud.com
 * @DATE 2021/7/7 11:41
 * @Desc
 * @Remarks
 */

/**
 * 用户名密码校验
 */
@Service
public class PhoneNumAndPasswordLogin extends AbstractLogin {

    @Autowired
    private ITbMemberService iTbMemberService;

    @Override
    public Integer getLoginType() {
        return LoginType.PHONE_PASS.getCode();
    }

    @Override
    public TbMember validate(LoginDto authLoginDto) {
        String phone = authLoginDto.getPhone();
        String password = authLoginDto.getPassword();
        if (!StringUtils.hasLength(phone) || !StringUtils.hasLength(password)) {
            throw new BizException("500", "手机号密码不能为空");
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("phone", phone);
        List<TbMember> list = iTbMemberService.list(queryWrapper);
        if (list == null || list.size() == 0) {
            throw new BizException("500", "用户不存在");
        }
        if (!DigestUtils.md5DigestAsHex(authLoginDto.getPassword().getBytes()).equals(list.get(0).getPassword())) {
            throw new BizException("手机号或者密码错误");
        }
        return list.get(0);
    }

    @Override
    public TbMember doProcessor(LoginDto authLoginDto) {
        return null;
    }
}
